using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TaskSystemClient
{
    public partial class ChooseItems : Form
    {
        public ChooseItems()
        {
            InitializeComponent();
        }
        private Dictionary<string, TreeNode> nodeMap = new Dictionary<string, TreeNode>();
        private string[] items;

        public string[] Items
        {
            get { return items; }
            set 
            { 
                items = value;
                tvItems.Nodes.Clear();
                nodeMap.Clear();
                foreach (string s in items)
                {
                    if (string.IsNullOrEmpty(s) || nodeMap.ContainsKey(s))
                        continue;
                    nodeMap.Add(s, tvItems.Nodes.Add(s));
                }
            }
        }
        public string[] SelectedItems
        {
            get
            {
                List<String> ret = new List<string>();
                foreach (KeyValuePair<string, TreeNode> kv in nodeMap)
                {
                    if (kv.Value.Checked)
                        ret.Add(kv.Key);
                }
                return ret.ToArray();
            }
            set
            {
                if (value == null)
                    return;
                //unselect all
                foreach (KeyValuePair<string, TreeNode> kv in nodeMap)
                    kv.Value.Checked = false;
                foreach (string s in value)
                {
                    if (nodeMap.ContainsKey(s))
                        nodeMap[s].Checked = true;
                }
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Location = GameEditor.Common.Utilities.GetPopupPosition(Cursor.Position, Size);
        }

        private void btnOK_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void tvItems_AfterCheck(object sender, TreeViewEventArgs e)
        {
            tbItemsPreview.Text = StringListSerializer.SeparatedByComma(SelectedItems);
        }
    }
}